<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>模拟登陆注册</title>
    <style>
        .container {
            width: 300px;
            height: 300px;
            margin: 200px auto;
        }

        h2 {
            text-align: center;
        }

        input {
            width: 100%;
            margin-top: 10px;
        }
    </style>
</head>

<body>
    <div class="container">
        <h2>财务系统</h2>
        <input type="text" placeholder="用户名" id="username">
        <input type="password" placeholder="密码" id="password">
        <input type="button" id="login" value="登陆">
        <input type="button" id="reg" value="注册">
    </div>
    <script>
        const reg = document.querySelector('#reg')
        const login = document.querySelector('#login')

        const username = document.querySelector('#username')
        const password = document.querySelector('#password')

        login.addEventListener('click', loginData)
        reg.addEventListener('click', regData)



        function loginData() {
            //     const data = {
            //     usernameValue: username.value,
            //     passwordValue: password.value
            // }
            // console.log(data.username)
            const url = 'http://localhost:8080/login?username=' + username.value + '&password=' + password.value
            console.log(url)

            fetch(url).then((res) => {
                console.log(res)
                //自定义返回结果和操作
                if (res.status === 201) {
                    alert("用户名不存在")
                } else if (res.status === 202) {
                    alert("密码错误")
                }else if(res.status === 200){
                    alert("登录成功!!!!!!!!")
                    console.log(location.href)
                    location.href='get.html'
                }
            })
        }

        function regData(){
            const data = `username=${username.value}&password=${password.value}`
            try {
                fetch('http://localhost:8080/reg', {
                    method: 'post',
                    body: data
                }).then((res)=>{
                    if(res.status === 201){
                        alert('账户已经存在')
                    }else if(res.status === 200){
                        alert('注册成功')
                        location.href=''
                    }
                })
            } catch (error) {
                
            }
        }
    </script>
</body>

</html>